package cn.wolfcode.shop.cloud.service;

import cn.wolfcode.shop.cloud.common.BusinessException;
import cn.wolfcode.shop.cloud.key.KeyPrefix;
import cn.wolfcode.shop.cloud.msg.RedisCodeMsg;
import com.alibaba.fastjson.JSON;
import org.apache.tomcat.util.modeler.FeatureInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class RedisService {
    @Autowired
    private JedisPool jedisPool;

    //set
    public <T> void set(KeyPrefix keyPrefix, String key, T data) {
        //realkey=前缀+key;
        try (Jedis jedis = jedisPool.getResource()) {
            //自动去拼接key
            String realKey = keyPrefix.getKeyPrefix() + key;
            //如果有过期时间再放到redis的时候要设置过期时间
            if (keyPrefix.getExpireTime() > 0) {
                jedis.setex(realKey, keyPrefix.getExpireTime(), JSON.toJSONString(data));
            } else {
                jedis.set(realKey, JSON.toJSONString(data));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    //get
    public <T> T get(KeyPrefix keyPrefix, String key, Class<T> clz) {
        //realkey=前缀+key;
        try (Jedis jedis = jedisPool.getResource()) {
            String realKey = keyPrefix.getKeyPrefix() + key;
            String redisValue = jedis.get(realKey);
            if (StringUtils.isEmpty(redisValue)) {
                return null;
            }
            return JSON.parseObject(redisValue, clz);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }


    //incr
    public <T> long incr(KeyPrefix keyPrefix, String key) {
        //realkey=前缀+key;
        try (Jedis jedis = jedisPool.getResource()) {
            String realKey = keyPrefix.getKeyPrefix() + key;
            return jedis.incr(realKey);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }


    //decr
    public <T> long decr(KeyPrefix keyPrefix, String key) {
        //realkey=前缀+key;
        try (Jedis jedis = jedisPool.getResource()) {
            String realKey = keyPrefix.getKeyPrefix() + key;
            return jedis.decr(realKey);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    //expire
    public <T> long expire(KeyPrefix keyPrefix, String key) {
        //realkey=前缀+key;
        try (Jedis jedis = jedisPool.getResource()) {
            String realKey = keyPrefix.getKeyPrefix() + key;
            return jedis.expire(realKey, keyPrefix.getExpireTime());
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }


    //exists
    public <T> boolean exists(KeyPrefix keyPrefix, String key) {
        //realkey=前缀+key;
        try (Jedis jedis = jedisPool.getResource()) {
            String realKey = keyPrefix.getKeyPrefix() + key;
            return jedis.exists(realKey);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }


    //hset
    public <T> void hset(KeyPrefix keyPrefix, String key, String field, T data) {
        //realkey=前缀+key;
        try (Jedis jedis = jedisPool.getResource()) {
            //自动去拼接key
            String realKey = keyPrefix.getKeyPrefix() + key;
            //如果有过期时间再放到redis的时候要设置过期时间
            jedis.hset(realKey, field, JSON.toJSONString(data));
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    //hget
    public <T> T hget(KeyPrefix keyPrefix, String key, String field, Class<T> clz) {
        //realkey=前缀+key;
        try (Jedis jedis = jedisPool.getResource()) {
            //自动去拼接key
            String realKey = keyPrefix.getKeyPrefix() + key;
            //如果有过期时间再放到redis的时候要设置过期时间
            if (StringUtils.isEmpty(realKey)) {
                return null;
            }
            return JSON.parseObject(jedis.hget(realKey, field), clz);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    //hgetAll
    public <T> Map<String, T> hgetAll(KeyPrefix keyPrefix, String key, Class<T> clz) {
        //realkey=前缀+key;
        try (Jedis jedis = jedisPool.getResource()) {
            //自动去拼接key
            String realKey = keyPrefix.getKeyPrefix() + key;
            //如果有过期时间再放到redis的时候要设置过期时间
            if (StringUtils.isEmpty(realKey)) {
                return null;
            }
            Map<String, String> stringStringMap = jedis.hgetAll(realKey);
            HashMap<String, T> data = new HashMap<>();
            for (Map.Entry<String, String> item : stringStringMap.entrySet()) {
                data.put(item.getKey(), JSON.parseObject(item.getValue(), clz));
            }
            return data;
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }


}
